<?php
/**
 * User: 1971229701@qq.com
 * Date: 2020/11/4/004
 * Time: 10:42
 */

namespace app\admin\controller;


use app\admin\logic\ListsLogic;
use app\common\model\Cate;
use app\common\model\Lists as ListsModel;
use think\App;
use think\Db;
use think\Request;

class Lists extends BaseAdmin
{
    private $cate;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->cate = Cate::all();
    }

    public function listing()
    {
        return $this->fetch();
    }

    public function ajax()
    {
        $title = $this->_get('title', '');
        $limit = $this->_get('limit', 10);


        $r = (new ListsLogic())->listing($title, $limit);
        return $r;
    }

    public function editsort()
    {
        $state = array();
        $id = $_POST['id'];
        $des = $_POST['des'];
        $res = Db::table('lists')->where(array('id'=>$id))->update(array('sort'=>$des));
        if( $res ) {
            return 1;
        }else{
            return 2;
        }
    }

    public function create(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $logic = new ListsLogic();
            if ($logic->create($data) === false) {
                return apiReturnFail($logic->getError());
            }
            return apiReturnSuc();
        } else {
            $configure = get_arr(ListsModel::$configure);
            $seat = get_arr(ListsModel::$seat);
            $drive = get_arr(ListsModel::$drive);
            $price = get_arr(ListsModel::$price);
            $liang_dian = get_arr(ListsModel::$liang_dian);
            $this->assign('cate', $this->cate);
            $this->assign('configure', $configure);
            $this->assign('seat', $seat);
            $this->assign('drive', $drive);
            $this->assign('price', $price);
            $this->assign('liang_dian', $liang_dian);

            return $this->fetch();
        }
    }

    public function update(Request $request)
    {

        $logic = new ListsLogic();
        if ($request->isPost()) {
            $data = $request->post();
            if ($logic->update($data) === false) {
                return apiReturnFail($logic->getError());
            }
            return apiReturnSuc();
        } else {

            $id = $this->_get('id');
            $r = $logic->update($id, 'get');
            $configure = get_arr(ListsModel::$configure);
            $seat = get_arr(ListsModel::$seat);
            $drive = get_arr(ListsModel::$drive);
            $price = get_arr(ListsModel::$price);
            $liang_dian = get_arr(ListsModel::$liang_dian);
            $this->assign('cate', $this->cate);
            $this->assign('configure', $configure);
            $this->assign('seat', $seat);
            $this->assign('drive', $drive);
            $this->assign('price', $price);
            $this->assign('liang_dian', $liang_dian);
            $this->assign('info', $r);
            return $this->fetch();
        }
    }

    public function delete($id)
    {
        $r = (new ListsLogic())->delete($id);
        return apiReturnSuc($r);
    }

    // 排序移动
    public function move($id, $type)
    {
        $logic = new ListsLogic();
        $r = $logic->move($id, $type);
        if ($r === false) {
            return apiReturnFail($logic->getError());
        }
        return apiReturnSuc();
    }

    // 多图上传
    public function many_upload($id = '')
    {
        if ($this->request->isPost()) {
            $images = $this->_post('images');
            $logic = new ListsLogic();
            $r = $logic->many_upload($id, $images);
            if ($r === false) {
                return apiReturnFail($logic->getError());
            }
            return apiReturnSuc();
        } else {
            $many_images = ListsModel::field('many_images')->get($id);
            $images_arr = $many_images->many_images_arr;
            $this->assign('id', $id);
            $this->assign('images_arr', $images_arr);
            return $this->fetch();
        }

    }
}